package com.itboye.sunsun;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsunhome.R;
import com.umeng.socialize.utils.Log;

/**
 * 日志名  格式   android_用户ID_yyyy-MM-dd
 * 
 * @author young
 *
 */
public class SplashActivity extends BaseActivity {

	Handler handler;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);

		 
		setContentView(R.layout.activity_splash);

		setStatusBarColor(R.color.splash);
		handler = new Handler() {
			public void handleMessage(android.os.Message msg) {
//				boolean IS_LOGINED = (Boolean) SPUtils.get(App.ctx, null, SPContants.IS_LOGINED, false);
//				if (IS_LOGINED) {
//					LoginController.setLoginState(new LoginedState());
//				} else {
//					LoginController.setLoginState(new UnLoginState());
//				}
				Log.v("languge", SPUtils.get(App.ctx, null, SPContants.LANGUAGE, "")+"");
				if(SPUtils.get(App.ctx, null, SPContants.LANGUAGE, "").equals("")){
					SPUtils.put(App.ctx, null, SPContants.LANGUAGE, "chinese");
					SPUtils.put(App.ctx, null, SPContants.ENGLISH, "english");
				}
				if(SPUtils.get(App.ctx, null, SPContants.LANGUAGE, "").equals("chinese"))//中文
					startActivity(new Intent(getApplicationContext(), MainHomeActivity.class));
				else//English
					startActivity(new Intent(getApplicationContext(), MainHomeEnglishActivity.class));
			};
		};
		// handler.sendEmptyMessageDelayed(123, 2500);
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

			try {
				final File dir = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + File.separator
						+ "SunSunCrash" + File.separator);

				
				if (dir.exists() && dir.isDirectory()) {
					File fs[] = dir.listFiles();
					StringBuilder sb = new StringBuilder();
					for (File file : fs) {
						BufferedReader reader = new BufferedReader(new FileReader(file));
						String line;
						while ((line = reader.readLine()) != null) {
							sb.append(line);
							sb.append("\n");
						}

						reader.close();
						sb.append("\n================================\n");
						 

					}

					 
					MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
							.typeKey("BY_Log_add")
							.apiVer("100")
							
							.param("notes_name", "android_"
									+ (String) SPUtils.get(getApplicationContext(), null, SPContants.USER_ID, "")+"_"+paserTime(System.currentTimeMillis()))

							.param("notes", sb.toString())
							.requestListener(new XRequestListener<String>() {
								
								@Override
								public void onResponse(String arg0) {
									if (dir.exists() && dir.isDirectory()) {
										File fs[] = dir.listFiles();
										for (File file : fs) {
											file.delete();
										}
										dir.delete();
									}
								}
							}).build();

					HttpRequest.getDefaultRequestQueue().add(request);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	 private String paserTime(long milliseconds) {  
	        System.setProperty("user.timezone", "Asia/Shanghai");  
	        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");  
	        TimeZone.setDefault(tz);  
	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	        String times = format.format(new Date(milliseconds));  
	          
	        return times;  
	    }  
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		handler.sendEmptyMessageDelayed(123, 2500);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		handler.removeMessages(123);
		finish();
		
	}
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		overridePendingTransition(0, R.anim.anim_falldown);
	}
}
